﻿using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Epitech.Krisk.Xna.UI.Game.InGame;
using Epitech.Krisk.Xna.ScreenManagerLib.ScreenManager;

namespace Epitech.Krisk.Xna.UI.Game.Utils
{
    public class RenderMovingBackground : AGameObject
    {
        protected String _bg;
        protected Texture2D _background;
        public static Rectangle Areabg;
        public static Rectangle Dest;
        double _winRatio;
        double _bgRatio;

        public RenderMovingBackground(string pathbg)
        {
            _bg = pathbg;
        }

        public override void LoadContent()
        {
            _background = Content.Load<Texture2D>(_bg);
            InitializeArea();
        }

        protected void InitializeArea()
        {
            Dest = new Rectangle(0, 0, ViewPort.Width, ViewPort.Height);
            _winRatio = ViewPort.Width / (double)ViewPort.Height;
            _bgRatio = _background.Width / (double)_background.Height;

            if (_winRatio < _bgRatio)
                Areabg = new Rectangle((_background.Width / 2 - (int)(_background.Height * _winRatio) / 2), 0,
                                        (int)(_background.Height * _winRatio), _background.Height);
            else if (_winRatio >= _bgRatio)
                Areabg = new Rectangle(0, (_background.Height / 2 - (int)(_background.Width / _winRatio) / 2),
                                       _background.Width, (int)(_background.Width / _winRatio));
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            // Poulet
            // Cette methode ne sert strictement a rien dans le cas present, donc je met un poney a la place :
            //         .,,.
            //       ,;;*;;;;,
            //      .-'``;-');;.
            //     /'  .-.  /*;;
            //   .'    \d    \;;               .;;;,
            //  / o      `    \;    ,__.     ,;*;;;*;,
            //  \__, _.__,'   \_.-') __)--.;;;;;*;;;;,
            //   `""`;;;\       /-')_) __)  `\' ';;;;;;
            //      ;*;;;        -') `)_)  |\ |  ;;;;*;
            //      ;;;;|        `---`    O | | ;;*;;;
            //      *;*;\|                 O  / ;;;;;*
            //     ;;;;;/|    .-------\      / ;*;;;;;
            //    ;;;*;/ \    |        '.   (`. ;;;*;;;
            //    ;;;;;'. ;   |          )   \ | ;;;;;;
            //    ,;*;;;;\/   |.        /   /` | ';;;*;
            //     ;;;;;;/    |/       /   /__/   ';;;
            //     '*jgs/     |       /    |      ;*;
            //          `""""`        `""""`     ;'

        }

        public void Move(int x, int y)
        {
            Rectangle tmp = new Rectangle(Areabg.Left + x, Areabg.Top + y, Areabg.Width, Areabg.Height);
            if (tmp.Top >= 0
                && tmp.Bottom <= _background.Height
                && tmp.Left >= 0
                && tmp.Right <= _background.Width)
                Areabg = tmp;
        }

        public void Zoom(int zoom)
        {
            int x, y, width, height;

            //
            width = (int)((Areabg.Height + zoom) * _winRatio);
            height = Areabg.Height + zoom * 2;
            x = Areabg.Left + (Areabg.Width - width) / 2;
            y = Areabg.Top - zoom;

            // Recentrage
            x = x < 0 ? 0 : x;
            y = y < 0 ? 0 : y;
            if (x + width > _background.Width)
                x = _background.Width - width;
            if (y + height > _background.Height)
                y = _background.Height - height;

            // Limites Min & Max
            if ((width <= _background.Width && height <= _background.Height)
                && (width >= _background.Width / 3 && height >= _background.Height / 3)) // Zoom limit
                Areabg = new Rectangle(x, y, width, height);
        }
        public override void UnloadContent()
        {
        }

        public override void HandleInput(InputState input)
        {
            PlayerIndex tmp;

            if (_background != null)
            {
                if (input.IsKeyPress(Keys.W, null, out tmp)
                    || input.IsButtonPress(Buttons.RightThumbstickUp, null, out tmp)
                    || input.SwipeState == SwipeState.Top)
                {
                    Move(0, -30);
                }
                if (input.IsKeyPress(Keys.S, null, out tmp)
                    || input.IsButtonPress(Buttons.RightThumbstickDown, null, out tmp)
                    || input.SwipeState == SwipeState.Bottom)
                {
                    Move(0, 30);
                }
                if (input.IsKeyPress(Keys.A, null, out tmp)
                    || input.IsButtonPress(Buttons.RightThumbstickLeft, null, out tmp)
                    || input.SwipeState == SwipeState.Left)
                {
                    Move(-30, 0);
                }
                if (input.IsKeyPress(Keys.D, null, out tmp)
                    || input.IsButtonPress(Buttons.RightThumbstickRight, null, out tmp)
                    || input.SwipeState == SwipeState.Right)
                {
                    Move(30, 0);
                }
                if (input.IsKeyPress(Keys.Up, null, out tmp)
                    || input.IsButtonPress(Buttons.RightTrigger, null, out tmp)
                    || input.IsZoom)
                {
                    Zoom(-30);
                }
                if (input.IsKeyPress(Keys.Down, null, out tmp)
                   || input.IsButtonPress(Buttons.LeftTrigger, null, out tmp))
                {
                    Zoom(30);
                }
            }
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sp = SpriteBatch;

            if (_background != null)
            {
                sp.Begin();
                sp.Draw(_background, Dest, Areabg, Color.White);
                sp.End();
            }
        }
    }
}
